#include "Main/stdafx.h"
#include "MJPlayer.h"
#include "MJSoundFun.h"
#include "Main/configConst.h";

MJPlayer::MJPlayer(int iIdex,cocos2d::Node* pSeatNode )
	:GamePlayer(NULL)
	,m_iIdex(iIdex)
	,m_userMoney(0)
	,m_pSeatNode(pSeatNode)
	, m_headUrl("")
{
	init();
}

MJPlayer::~MJPlayer()
{

}

void MJPlayer::init()
{

	m_uPanel = (Layout*)m_pSeatNode->getChildByName(StringUtils::format("Panel_player%d", m_iIdex));
	m_uPanel->setVisible(false);

	m_head = (Sprite*)m_uPanel->getChildByName("Sprite_head");
	m_headFrame = (Sprite*)m_uPanel->getChildByName("Sprite_headk");
	m_offLine_sprite = (Sprite*)m_uPanel->getChildByName("Sprite_offline");
	m_que_sprite = (Sprite*)m_uPanel->getChildByName("Sprite_que");
	m_ready_sprite = (Sprite*)m_uPanel->getChildByName("Sprite_ready");
	m_userInfo_money = (Text*)m_uPanel->getChildByName("Text_score");

	m_addScore = (Text*)m_uPanel->getChildByName("Text_AddScore");

	PlayerLeave();
}
int  MJPlayer::getIdex()
{
	return m_iIdex;
}
void MJPlayer::defaultState()
{
	m_que_sprite->setVisible(false);
	m_ready_sprite->setVisible(false);
	m_offLine_sprite->setVisible(false);
	m_addScore->setVisible(false);

	upPlayerState();
}

void MJPlayer::startGame()
{
	m_ready_sprite->setVisible(false);
}

void MJPlayer::EndGame()
{
	if (m_pUserItem == NULL )
	{
		return;
	}
}

void MJPlayer::PlayerEnter()
{
	m_uPanel->setVisible(true);
	m_offLine_sprite->setVisible(false);

	std::string updateHeadUrl = StringUtils::format("%s/AvatarInterface.aspx?type=getList&id=%d", bzyfk_url.c_str(), GetUserID());
	MCWebReq::instance().sendRequestDocumentUrl(updateHeadUrl, nullptr, CC_CALLBACK_1(MJPlayer::Rsp_uptHeadUrl_list, this));
}

void MJPlayer::PlayerLeave()
{
	defaultState();
	m_userMoney = 0;
	m_headUrl = "";
	m_uPanel->setVisible(false);
}

bool MJPlayer::checkUserSitDown()
{
	return m_uPanel->isVisible();
}

void MJPlayer::upPlayerInfo()
{
	m_userInfo_money->setString(StringUtils::format("%lld", m_userMoney));
}
void MJPlayer::setZhuang()
{
	m_que_sprite->setVisible(true);
}
void MJPlayer::showAddGold(SCORE iGold)
{
	m_addScore->setVisible(true);
	if (iGold >= 0)
	{
		m_addScore->setTextColor(cocos2d::ccColor4B(183, 12, 1, 255));
		m_addScore->setString(StringUtils::format("+%lld", iGold));
	}
	else
	{
		m_addScore->setTextColor(cocos2d::ccColor4B(30, 144, 255, 255));
		m_addScore->setString(StringUtils::format("%lld", iGold));
	}
}

void MJPlayer::hideAddGold()
{
	m_addScore->setVisible(false);
}

void MJPlayer::upPlayerState()
{
	if (GetUserStatus() == US_READY)
	{
		m_ready_sprite->setVisible(true);
	}
	if (GetUserStatus() == US_OFFLINE)
	{
		m_offLine_sprite->setVisible(true);
	}
	else
	{
		m_offLine_sprite->setVisible(false);
	}
}

void MJPlayer::showChatWord(std::string szChatString)
{
	Vec2 endPos = m_uPanel->getPosition();
	Layout* chatPanel = NULL;
	if (m_iIdex > 6)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatRight");
		endPos.x -= m_uPanel->getContentSize().width;
		endPos.x -= chatPanel->getContentSize().width / 2;
	}
	else if (m_iIdex > 3)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.y -= m_uPanel->getContentSize().height;
		endPos.y -= chatPanel->getContentSize().height / 2;
	}
	else if (m_iIdex == 0)
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
		endPos.y += m_uPanel->getContentSize().height;
		endPos.y += chatPanel->getContentSize().height / 2;
	}
	else
	{
		chatPanel = (Layout*)m_pSeatNode->getChildByName("Image_chatLeft");
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
	}
	Layout* newPanel = (Layout*)chatPanel->clone();
	Text* chatText = (Text*)newPanel->getChildByName("Text_value");
	chatText->setString(szChatString);
	m_pSeatNode->addChild(newPanel);
	newPanel->setPosition(endPos);
	newPanel->runAction(Sequence::create(DelayTime::create(2.0f), RemoveSelf::create(), NULL));
}

void MJPlayer::showChatFace(word wItemIndex)
{
	Vec2 endPos = m_uPanel->getPosition();
	Sprite* chatPanel = Sprite::create(StringUtils::format("chat/face%d.png", wItemIndex + 1));
	if (m_iIdex > 6)
	{
		endPos.x -= m_uPanel->getContentSize().width;
		endPos.x -= chatPanel->getContentSize().width / 2;
	}
	else if (m_iIdex > 3)
	{
		endPos.y -= m_uPanel->getContentSize().height;
		endPos.y -= chatPanel->getContentSize().height / 2;
	}
	else if (m_iIdex == 0)
	{
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
		endPos.y += m_uPanel->getContentSize().height;
		endPos.y += chatPanel->getContentSize().height / 2;
	}
	else
	{
		endPos.x += m_uPanel->getContentSize().width;
		endPos.x += chatPanel->getContentSize().width / 2;
	}
	m_pSeatNode->addChild(chatPanel);
	chatPanel->setPosition(endPos);
	chatPanel->runAction(Sequence::create(DelayTime::create(2.0f), RemoveSelf::create(), NULL));
}

void MJPlayer::initUserMoney(SCORE money)
{
	m_userMoney = money;
	m_userInfo_money->setString(StringUtils::format("%lld", m_userMoney));
}

void MJPlayer::updateUserMoney(SCORE money)
{
	m_userMoney += money;
	m_userInfo_money->setString(StringUtils::format("%lld", m_userMoney));
}

void MJPlayer::Rsp_uptHeadUrl_list(const std::string& data)
{
	rapidjson::Document doc;
	doc.Parse<rapidjson::kParseDefaultFlags>(data.c_str());
	if (doc.HasParseError() || !doc.HasMember("Avatar"))
	{
		return;
	}
	std::string strUrl = doc["Avatar"].GetString();
	if (strUrl.find("http") != std::string::npos)
	{
		m_headUrl = strUrl;
		ImagicDownManager::Instance().addDown(m_head,
			strUrl, GetUserID());
	}
	else
	{
		if (GetGender() == 0)
			m_head->setTexture("common/Head0.png");
		else
			m_head->setTexture("common/Head1.png");

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
		m_head->setTexture("common/Head1.png");
#endif
	}
}

void MJPlayer::showDefaultSound(int itemIndex)
{
	MJSoundFun::playEffect(StringUtils::format("default/default%d", itemIndex));
}

